﻿using System;
using System.Collections.Generic;
using System.Text;
using Obvious.Framework.Notification.Interfaces;
using Obvious.Framework.Notification.Enums;
using System.Net.Mail;
using System.Net;
using System.Configuration;
using Obvious.Framework.Notification.ConfigHandlers;


namespace Obvious.Framework.Notification.Implementation
{
    internal class EmailNotificationProvider : INotificationProvider
    {
        #region Private Properties
        private string smtpHost;
        private int smtpPort;
        private string smtpUserName;
        private string smtpPassword;
        SmtpClient smtpClient;
        Exception exception;
        private string configSection = "payTxnNotificationGroup/payTxnNotificationSection";
        #endregion

        #region Private Properties
        SmtpClient SmtpClient
        {
            get
            {
                if (smtpClient == null)
                {
                    smtpClient = new SmtpClient(smtpHost);
                    smtpClient.Port = smtpPort;
                    if (smtpUserName != null && smtpUserName != string.Empty)
                    {
                        NetworkCredential credential = new NetworkCredential(smtpUserName, smtpPassword);
                        smtpClient.Credentials = credential;
                    }
                }
                return smtpClient;
            }
        }
        #endregion

        #region Constructors
        public EmailNotificationProvider()
        {
            /*PayTxnNotificationSection config = (PayTxnNotificationSection)ConfigurationManager.GetSection(this.configSection);
            if (config.Type == NotificationType.Email)
            {
                smtpHost = config.Email.SmtpHost;
                smtpPort = config.Email.SmtpPort;
                smtpUserName = config.Email.SmtpUserName;
                smtpPassword = config.Email.SmtpPwd;
            }
            else
            {
                throw new Exception("No configuration defined for email section");
            }*/
        }
        #endregion

        #region Public Properties
        Exception Exception
        {
            get
            {
                return exception;
            }
        }
        #endregion

        #region INotificationProvider Members

        public NotificationType Type
        {
            get { return NotificationType.Email; }
        }

        public bool Nofity(INotification notification)
        {
            return this.SendMail(notification.To, notification.From, notification.Subject, notification.Message);
        }

        public bool Notify(string To, string From, string Subject, string Message)
        {
            return this.SendMail(To, From, Subject, Message);
        }

        public bool Notify(string To, string From, string Subject, string Message, DateTime ScheduleSendDate)
        {
            bool bRetValue = true;
            if(DateTime.Now.Date.CompareTo(ScheduleSendDate.Date) <= 0)
                bRetValue = this.SendMail(To, From, Subject, Message);
            //else
            //write logic to put it in queue
            return bRetValue;
        }
        #endregion

        #region Private Members
        private bool SendMail(string To, string From, string Subject, string Message)
        {
            bool bRetValue = false;
            try
            {
                MailMessage message = new MailMessage(From, To, Subject, Message);
                SmtpClient.Send(message);
            }
            catch (Exception ex)
            {
                bRetValue = true;
                exception = ex;
            }
            return bRetValue;
        }
        #endregion
    }
}
